package model.displayer;

import controller.MainController;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
import model.Point;
import model.utils.Utils;

public class LineDisplayer extends ShapeDisplayerJFxImpl
{
    public static final Point start = new Point(10,10);
    public static final Point end = new Point(50,50);

    Line line;

    LineDisplayer(Pane canvas, MainController controller)
    {
        super(canvas,controller);
        this.shape = new Line();
        shape.setStroke(DEFAULT_STROKE);
        shape.setFill(DEFAULT_FILL);
        shape.setStrokeWidth(DEFAULT_STROKE_WIDTH);
        shape.setOnMouseClicked((MouseEvent e)->controller.handleShapeClicked(this));

        this.line = (Line)this.shape;
        this.line.setStartX(start.getX());
        this.line.setStartY(start.getY());
        this.line.setEndX(end.getX());
        this.line.setEndY(end.getY());
    }

    @Override
    protected void resize(double multi)
    {
        Point oldStart = new Point(this.line.getStartX(),this.line.getStartY());
        Point oldEnd = new Point(this.line.getEndX(),this.line.getEndY());
        Point center = Utils.calcMidPoint(oldStart,oldEnd);

        double XLen = line.getEndX()-center.getX();
        double YLen = line.getEndY()-center.getY();
        XLen *= (1 + multi);
        YLen *= (1 + multi);

        Point newStart = new Point(center.getX() - XLen , center.getY() - YLen);
        Point newEnd = new Point(center.getX() + XLen , center.getY() + YLen);

        setLocation(newStart,newEnd);
    }

    @Override
    public void setLocation(Point start, Point end)
    {
        this.line.setStartX(start.getX());
        this.line.setStartY(start.getY());
        this.line.setEndX(end.getX());
        this.line.setEndY(end.getY());
    }

    @Override
    public ShapeDisplayer clone()
    {
        LineDisplayer copy = new LineDisplayer(this.canvas,this.controller);
        copy.line.setStartX(this.line.getStartX());
        copy.line.setStartY(this.line.getStartY());
        copy.line.setEndX(this.line.getEndX());
        copy.line.setEndY(this.line.getEndY());
        copy.line.setStroke(this.line.getStroke());
        copy.line.setStrokeWidth(DEFAULT_STROKE_WIDTH);
        return copy;
    }

    @Override
    protected Point getCenter()
    {
        Point start = new Point(this.line.getStartX(),this.line.getStartY());
        Point end = new Point(this.line.getEndX(),this.line.getEndY());
        Point center = Utils.calcMidPoint(start,end);
        return center;
    }

    @Override
    protected void setCenter(Point newCenter)
    {
        Point center = getCenter();
        double XMove = newCenter.getX() - center.getX();
        double YMove = newCenter.getY() - center.getY();

        Point newStart = new Point(this.line.getStartX() + XMove,this.line.getStartY() + YMove);
        Point newEnd = new Point(this.line.getEndX() + XMove,this.line.getEndY() + YMove);

        setLocation(newStart,newEnd);
    }
}
